﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * ZIP Bit Flags Class
 * 		Represents and manages the bit flags values for the various header entries.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Collections;

namespace Adaptive.Foundation.IO.Zip
{
    /// <summary>
    /// Represents and manages the bit flags values for the various header entries.
    /// </summary>
    public sealed class ZipBitFlags : AiObjectBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Internal bit array.
        /// </summary>
        private BitArray _bitArray;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the <see cref="ZipBitFlags" /> class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public ZipBitFlags()
            : base()
        {
            _bitArray = new BitArray(new byte[] { 0, 0 });
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ZipBitFlags" /> class.
        /// </summary>
        /// <param name="value">
        /// The value containing the bitmap.
        /// </param>
        public ZipBitFlags(short value)
            : base()
        {
            byte[] data = null;

            data = new byte[2];
            data[0] = (byte)(value >> 8);
            data[1] = (byte)((value << 8) >> 8);

            _bitArray = new BitArray(data);

        }
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><b>true</b> to release both managed and unmanaged resources;
        /// <b>false</b> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            _bitArray = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets a value indicating whether a data descriptor follows the file entry.
        /// </summary>
        /// <value>
        /// <b>true</b> if this instance has data descriptor; otherwise, <b>false</b>.
        /// </value>
        public bool HasDataDescriptor
        {
            get
            {
                return _bitArray[2];
            }
        }
        /// <summary>
        /// Gets a value indicating whether the data file is encrypted.
        /// </summary>
        /// <value><c>true</c> if the data file is encrypted; otherwise, <c>false</c>.</value>
        public bool Encrypted
        {
            get
            {
                //Bit 0: If set, indicates that the file is encrypted
                return _bitArray[0];
            }
        }
        /// <summary>
        /// Gets the compression method used for the deflate algorithm.
        /// </summary>
        /// <value>
        /// A <see cref="DeflateCompressionMethods"/> enumerated value indicating the 
        /// mode of compression.
        /// </value>
        public DeflateCompressionMethods CompressionMethod
        {
            get
            {
                DeflateCompressionMethods returnValue;

                //(For Methods 8 and 9 - Deflating)
                //         Bit 2  Bit 1
                //           0      0    Normal (-en) compression option was used.
                //           0      1    Maximum (-exx/-ex) compression option was used.
                //           1      0    Fast (-ef) compression option was used.
                //           1      1    Super Fast (-es) compression option was used.


                if ((_bitArray[0]) && (_bitArray[1]))
                    returnValue = DeflateCompressionMethods.SuperFast;
                else if ((_bitArray[0]) && !(_bitArray[1]))
                    returnValue = DeflateCompressionMethods.Fast;
                else if ((!_bitArray[0]) && (_bitArray[1]))
                    returnValue = DeflateCompressionMethods.Maximum;
                else
                    returnValue = DeflateCompressionMethods.Normal;

                return returnValue;
            }
        }
        /// <summary>
        /// gets or sets the value.
        /// </summary>
        /// <value>
        /// The short integer value containing the bitmap.
        /// </value>
        public short Value
        {
            get
            {
                short returnValue = 0;
                byte[] data = null;

                data = new byte[2];
                _bitArray.CopyTo(data, 0);

                returnValue = (short)(((short)((data[0]) << 8)) + ((short)data[1]));
                return returnValue;
            }
            set
            {
                byte[] data = new byte[2];

                data[0] = (byte)(value >> 8);
                data[1] = (byte)((value << 8) >> 8);

                _bitArray = new BitArray(data);
            }
        }
        #endregion
    }
}